package com.lhy.admin.web;

import com.lhy.admin.Constants;
import com.lhy.admin.common.mvcplus.controller.SimpleCrudControllerSupport;
import com.lhy.admin.common.mvcplus.entity.RespResult;
import com.lhy.admin.common.secure.authentication.Authentication;
import com.lhy.admin.common.secure.authorization.Permissions;
import com.lhy.admin.common.treeable.Treeable;
import com.lhy.admin.common.treeable.TreeableUtil;
import com.lhy.admin.model.Dict;
import com.lhy.admin.model.Permission;
import com.lhy.admin.model.extend.Menu;
import com.lhy.admin.model.extend.PermissionGroup;
import com.lhy.admin.model.extend.PermissionTree;
import com.lhy.admin.model.extend.RolePermissions;
import com.lhy.admin.service.DictService;
import com.lhy.admin.service.PermissionService;
import com.lhy.admin.service.RolePermissionService;
import com.lhy.admin.util.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("permission")
@Permissions
public class PermissionController extends SimpleCrudControllerSupport<PermissionService, Permission, Integer> {

    @Autowired
    AuthUtil authUtil;

    @Autowired
    DictService dictService;

    @Autowired
    RolePermissionService rolePermissionService;

    @Override
    public RespResult<Permission> get(Integer id) {
        return super.get(id);
    }

    @RequestMapping(value = "listMenus", method = RequestMethod.GET)
    @Authentication
    @Deprecated
    public RespResult listMenus() {
        List<Permission> permissions = authUtil.getPermission();
        List<Menu> collect = permissions.stream()
                .map(permission -> new Menu(permission.getCode(), "/" + permission.getCode(), permission.getName(), permission.getLevel(), permission.getPriority()))
                .sorted(Comparator.comparing(Menu::getPriority))
                .collect(Collectors.toList());
        return RespResult.ok().setData(collect);
    }

    @RequestMapping(value = "listMenuTree", method = RequestMethod.GET)
    @Authentication
    public RespResult listMenuTree() {
        List<Permission> permissions = authUtil.getPermission().stream()
                //只查询页面权限
                .filter(permission -> permission.getType().equals(0))
                .sorted(Comparator.comparing(Permission::getPriority))
                .collect(Collectors.toList());
        return RespResult.ok().setData(TreeableUtil.toTree(permissions, PermissionTree.class));
    }

    @Permissions("permission.view")
    @RequestMapping(value = "tree", method = RequestMethod.GET)
    public RespResult<List<Treeable>> tree(@Validated @ModelAttribute Permission bean) {
        List<Permission> list = service.list(bean);
        return RespResult.ok(TreeableUtil.toTree(list, PermissionTree.class));
    }

    @Permissions("permission.view")
    @RequestMapping(value = "defaultPermissionGroup", method = RequestMethod.GET)
    public List<Dict> defaultPermissionGroup() {
        return dictService.list(new Dict().setCode(Constants.DEFAULT_PERMISSION_GROUP).setStatus(1));
    }

    @Permissions("permission.add")
    @RequestMapping(value = "group", method = RequestMethod.POST)
    public RespResult createGroup(@RequestBody PermissionGroup bean) {
        service.savePermissionWithGroup(bean);
        return RespResult.ok();
    }

    @Override
    @Permissions("permission.edit")
    public RespResult update(Permission bean) {
        if (bean.getParent() == null) {
            bean.setParent(0);
        }
        return RespResult.create(this.service.updateById(bean));
    }

    @Override
    @Permissions("permission.del")
    public RespResult delete(Integer id) {
        return RespResult.create(service.delPermission(id));
    }

    /**
     * @param name
     * @return java.util.List<com.lhy.admin.common.treeable.Treeable>
     * @throws
     * @Title: rolePermission
     * @Description: 查询角色的所有权限
     * @date 2018/6/12 17:30
     */
    @Permissions("permission.view")
    @RequestMapping(value = "rolePermission/{name}", method = RequestMethod.GET)
    public List<Permission> rolePermission(@PathVariable("name") String name) {
        return service.listPermissionsByRoleName(name);
    }

    /**
     * 更新角色权限关系
     */
    @Permissions({"permission.edit", "permission.add", "permission.del"})
    @RequestMapping(value = "updateRolePermissions", method = RequestMethod.POST)
    public RespResult updateRolePermissions(@RequestBody RolePermissions rolePermissions) {
        rolePermissionService.updateRolePermission(rolePermissions);
        return RespResult.ok();
    }

    @Override
    public RespResult validate(Permission bean) {
        return super.validate(bean);
    }
}
